package cn.wwcyc.controller;

import cn.wwcyc.dao.ModbusDao;
import cn.wwcyc.domain.*;
import cn.wwcyc.exception.WWExceptionEnum;
import cn.wwcyc.mapper.TjDevMapper;
import cn.wwcyc.mapper.TjHsyMapper;
import cn.wwcyc.mapper.TjSsHslMapper;
import cn.wwcyc.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.serotonin.modbus4j.BatchRead;
import com.serotonin.modbus4j.BatchResults;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.locator.BaseLocator;
import org.bouncycastle.asn1.eac.UnsignedInteger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hsy")
public class HSYController {

    @Autowired
    TjDwService tjDwService;
    @Autowired
    UserService userService;
    @Autowired
    TjHsyMapper tjHsyMapper;
    @Autowired
    TjHsyService tjHsyService;
    @Autowired
    @Qualifier("tjSsHslServiceImpl2")
    TjSsHslService tjSsHslService;
    @RequestMapping("/getjhsBydw")
    public R<List<String>> getJhsBydw(@RequestParam Long dwid){
        List<Long> dwIdlist = tjDwService.getSelfAndDescendantIdList(dwid);
        QueryWrapper<TjHsy> queryMapper = new QueryWrapper();
        queryMapper.in("dwid", dwIdlist);
        List<TjHsy> hsyList = tjHsyMapper.selectList(queryMapper);
        List<String> jhs = new ArrayList<>();
        for (TjHsy jh : hsyList) {
            jhs.add(jh.getJh());
        }
        return R.success(jhs);
    }



    //此分页函数中的返回对象中含有单位及设备类型对象数据
    @RequestMapping("/getpage")
    public R<IPage<TjHsyVo>> getPage(
            @RequestParam Integer pagenum,
            @RequestParam Integer pagesize,
            @RequestParam(required = false) Long dwid,
//            @RequestParam(required = false) Long devtype,
            @RequestParam(required = false) String jh,
            HttpServletRequest request
    ) {
        //计时
        long start = System.currentTimeMillis();
        System.out.println("--------------start:" + start);
//        System.out.println("dwid:" + dwid + "  devtype:" + devtype + "  devname:" + devname);
        IPage<TjHsyVo> page = new Page<>(pagenum, pagesize);
        QueryWrapper<TjHsy> qw = new QueryWrapper<>();
        if (!StringUtils.isBlank(jh)) {
            qw.like("jh", jh);
        }
        User user = userService.getCurrentUser(request);
        if(dwid == null)
            dwid = user.getDw();

        TjDw tjDw = null;
        if (dwid != null) {
            tjDw = tjDwService.getDwTreeById(dwid);
        }
        List<TjDw> dwlist = new ArrayList<>();
        dwlist = tjDwService.DwTreeToList(dwlist, tjDw);
        List<Long> dwIdList = new ArrayList<>();
        for (TjDw tjDw1 : dwlist) {
            dwIdList.add(tjDw1.getId());
        }
        if (dwIdList.size() > 0) {
            qw.in("dwid", dwIdList);
        }

        long count = tjHsyMapper.selectCount(qw);

        page.setTotal(count);
        page.setPages(page.getTotal() % page.getSize() == 0 ? page.getTotal() / page.getSize() : page.getTotal() / page.getSize() + 1);
        if (pagenum > page.getPages()) {
            page.setCurrent(page.getPages());
        }
        if (pagenum < 1) {
            page.setCurrent(1);
        }
        Long beforepageTime = System.currentTimeMillis();
        System.out.println("-------------beforepageTime:" + (beforepageTime-start));
//        System.out.println("pagenum:" + pagenum + "  pagesize:" + pagesize + " current:" + page.getCurrent() + "  size:" + page.getSize() + " pages:" + page.getPages());
        List<TjHsyVo> list = tjHsyMapper.selectPagewithObj(page.getCurrent(), page.getSize(), dwIdList, jh);
        page.setRecords(list);
        Long end = System.currentTimeMillis();
        System.out.println("-------------pageTime:" + (end - beforepageTime));
        System.out.println("---------------------耗时：" + (end - start));
        return R.success(page);
    }



    @GetMapping("/getById")
    public R<TjHsy> getById(@RequestParam Long id) {
        TjHsy hsy = tjHsyMapper.selectById(id);
        return R.success(hsy);
    }

    @PostMapping("/save")
    public R save(@RequestBody TjHsy hsy) {
        tjHsyMapper.insertHsy(hsy);//这个能自动返回主键id
//        System.out.println("save"+tjSsHslService);
        tjSsHslService.addHsy(hsy);
        return R.success();
    }

    //还未实现！！！！！！！！！！！！！！！！！！！！！！！！！
    @GetMapping("/copy")
    public R copy(@RequestParam Long id, @RequestParam Integer count) {
        tjHsyService.copy(id, count);
//        tjDevMapper.insert(dev);
        return R.success();
    }

    @PostMapping("/edit")
    public R edit(@RequestBody TjHsy hsy) {
        tjHsyMapper.updateById(hsy);
        tjSsHslService.editHsy(hsy);
        return R.success();
    }

    //
    @DeleteMapping
    public R del(Long id) {
//        System.out.println("HSYController.del------------"+id);
        tjHsyMapper.deleteById(id);
        tjSsHslService.delHsyById(id);
        return R.success();
    }

    @PostMapping("delbatch")
    public R delbatch(@RequestBody List<TjHsy> list) {
        for (TjHsy hsy : list) {
            Long id = hsy.getId();
            tjHsyMapper.deleteById(id);
            tjSsHslService.delHsyById(id);
        }
        return R.success();
    }


}
